package com.neuedu.mvc.util;

import com.alibaba.fastjson.JSON;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class JSONUtils {


    /**
     * 想内存对象转换成json字符串
     * @param response
     * @param data
     * @throws IOException
     */
    public static void write(HttpServletResponse response ,Object data ) throws IOException {
        //list -->  String(JSON)    vuejs拿到string 转换成JS 认识的JSON对象
        String json = JSON.toJSONString(data);


        //设置ContentType
        response.setContentType("application/json;charset=utf-8");
        response.setCharacterEncoding("utf-8");

        PrintWriter out = response.getWriter();

        out.write(json);
        out.flush();
        out.close();
    }


}
